﻿using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StartsEAS_API.BLL.Interfaces.IScheduleB;
using StartsEAS_API.DAL.ScheduleD;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.BLL.ScheduleB
{
    public class ScheduleBLL : IScheduleBLL
    {
        private readonly StarsEASContext _context;

        public ScheduleDAL _scheduleDal;

        public ScheduleBLL(IServiceScopeFactory context)
        {
            _context = context.CreateScope().ServiceProvider.GetRequiredService<StarsEASContext>();
            _scheduleDal = new(_context);
        }

        public ResultUtils GetSchedultByClassId(string classId)
        {
            return _scheduleDal.GetSchedultByClassId(classId);
        }

        public ResultUtils GetSchedultBySchedulePlace(string SchedulePlace)
        {
            return _scheduleDal.GetSchedultBySchedulePlace(SchedulePlace);
        }

        public ResultUtils GetSchedultByWorkerId(string token)
        {
            return _scheduleDal.GetSchedultByWorkerId(token);
        }

        public ResultUtils InsertSchedule(JArray data)
        {
            List<Schedule> scheduleList = new List<Schedule>();
            foreach (var item in data)
            {
                string jsonStr = JsonConvert.SerializeObject(item);
                Schedule cou = JsonConvert.DeserializeObject<Schedule>(jsonStr);
                scheduleList.Add(cou);
            }
            return _scheduleDal.InsertSchedule(scheduleList);
        }

        public ResultUtils GetSchedultByToken(string token) { 
            return _scheduleDal.GetSchedultByToken(token);
        }

        public ResultUtils GetWorkerEvaluationByStudent(string token,int Semester)
        {
            return _scheduleDal.GetWorkerEvaluationByStudent(token, Semester);
        }

        public ResultUtils AdjustTheCourse(Schedule data)
        {
            int result = _scheduleDal.AdjustTheCourse(data);
            if (result == 0)
            {
                return new ResultUtils()
                {
                    code = 500,
                    message = "老师或班级或教室占用，调节失败"
                };
            }
            else {
                return new ResultUtils() { code = 200, message = "调课成功" };
            }
        }
    }
}
